package org.budo.dubbo.protocol.http.exporter;

import com.alibaba.dubbo.config.ServiceConfig;
import lombok.extern.slf4j.Slf4j;
import org.budo.dubbo.protocol.http.aop.webapi.WebApi;
import org.budo.dubbo.protocol.http.exception.BudoRuntimeException;
import org.budo.dubbo.protocol.http.exporter.util.ExportUtil;

import java.util.Map;


@Slf4j
public class BudoDubboHttpAnnotationExporter extends BudoDubboHttpExporter {

    @Override
    public void afterPropertiesSet() {
        Map<String, Object> beanMap = super.getApplicationContext().getBeansWithAnnotation(WebApi.class);
        for (Object value : beanMap.values()) {
            exportService(value);
        }
    }

    private void exportService(Object beanRef) {
        Class<?> interfaceType = beanRef.getClass().getSuperclass().getInterfaces()[0];

        try {
            ServiceConfig<Object> serviceConfig = ExportUtil.newServiceConfig(interfaceType.getName(), beanRef);
            serviceConfig.setValidation(this.getValidation());
            serviceConfig.setFilter(this.getFilter());
            serviceConfig.export();
            log.info("#30 exported, interfaceType=" + interfaceType + ", beanRef=" + beanRef.getClass() + "@" + beanRef.hashCode());
        } catch (Throwable var4) {
            log.error("#32 exportService error, interfaceType=" + interfaceType + ", e=" + var4, var4);
            throw new BudoRuntimeException("#33 exportService error, interfaceType=" + interfaceType + ", e=" + var4, var4);
        }
    }
}
